package com.xtaller.easy.admin.mapper;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.xtaller.easy.admin.model.SysLog;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* Created by Taller on 2018/05/11
*/
@Mapper 
public interface SysLogMapper extends BaseMapper<SysLog> {
    @Select("SELECT * FROM v_log a " +
            "JOIN (SELECT id from v_log where 1=1 ${where} " +
            "LIMIT #{index}, #{size})b ON a.id=b.id order by `createTime` desc ")
    List<JSONObject> getPage(@Param("where") String where,
                             @Param("index") int index,
                             @Param("size") int size);

    @Select("select count(1) total from v_log where 1=1 ${where} ")
    JSONObject getPageCount(@Param("where") String where);

    @Select("select classMethod,method,count(1) total from sys_log " +
            "where classMethod<>'' and FROM_UNIXTIME(createTime, '%Y-%m-%d') = DATE_SUB(curdate(),INTERVAL ${day} DAY) " +
            "group by classMethod,method order by classMethod")
    List<JSONObject> getApiLog(@Param("day") int day);
    @Select("select classMethod,method,count(1) total from sys_log where classMethod<>'' " +
            "group by classMethod,method order by classMethod")
    List<JSONObject> getApiLogs();
    
    @Select("select " +
            "  (select count(1) " +
            "   from base_user " +
            "   where isDel = 0) total, " +
            "  (select count(1) from base_user where isDel=0 and FROM_UNIXTIME(createTime, '%Y-%m-%d') = DATE_SUB(curdate(), INTERVAL 1 DAY)) yesterday, " +
            "  (select count(1) from base_user where isDel=0 and FROM_UNIXTIME(createTime, '%Y-%m-%d') = DATE_SUB(curdate(), INTERVAL 0 DAY)) today, " +
            "  (select count(distinct userId) count " +
            "    from sys_log where type=3 and FROM_UNIXTIME(createTime, '%Y-%m-%d') = DATE_SUB(curdate(), INTERVAL 1 DAY)) loginCount")
    JSONObject getCount();
}